package com.zyw.service.repertory.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.Order;
import com.zyw.entity.RepertoryFinish;
import com.zyw.mapper.repertory.RepertoryFinishMapper;
import com.zyw.service.order.OrderService;
import com.zyw.service.repertory.RepertoryFinishService;
import com.zyw.util.UnitConversionUtil;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RepertoryFinishServiceImpl implements RepertoryFinishService {

    @Resource
    public RepertoryFinishMapper repertoryFinishMapper;

    @Autowired
    public OrderService orderService;

    @Override
    @Transactional
    public void save(RepertoryFinish repertoryFinish) {

        repertoryFinish.setPutDate(UnitConversionUtil.nowDate());
        repertoryFinish.setStatus(0);

        repertoryFinishMapper.insertSelective(repertoryFinish);
    }

    @Override
    public LayUITableVO<List<RepertoryFinish>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));


        List<RepertoryFinish> list = repertoryFinishMapper.findPage(map);
        for (RepertoryFinish repertoryFinish : list) {
            repertoryFinish.setPutDate(UnitConversionUtil.conversionDate(repertoryFinish.getPutDate()));
            repertoryFinish.setComeDate(UnitConversionUtil.conversionDate(repertoryFinish.getComeDate()));

            if (repertoryFinish.getStatus() == 0){
                repertoryFinish.setStatusString("入库中");
            }else {
                repertoryFinish.setStatusString("已出库");
            }
        }

        PageInfo<RepertoryFinish> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<RepertoryFinish>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;

    }

    @Override
    @Transactional
    public void update(RepertoryFinish repertoryFinish) {
        repertoryFinish.setStatus(1);
        repertoryFinishMapper.updateByPrimaryKeySelective(repertoryFinish);

        Order order = new Order();
        order.setId(repertoryFinish.getId());
        order.setStatus(4);
        orderService.update(order);

    }
}
